#include "kmysql.h"
#include "log.h"
#include <stdio.h>
#include <string.h>

MYSQL *MysqlConn(char *userName, char *passwd, char *dbName)
{
    if (userName == NULL || passwd == NULL || dbName == NULL)
        return NULL;

    MYSQL *conn = NULL;
    conn = mysql_init(NULL);
    if (conn == NULL)
    {
        fprintf(stderr, "mysql_init error\n");
        return NULL;
    }

    if (mysql_real_connect(conn, "localhost", userName, passwd, dbName, 0, NULL, 0) == NULL)
    {
        PrintError(conn, "mysql_real_connect error");
        mysql_close(conn);
        return NULL;
    }

    return conn;
}

void PrintError(MYSQL *conn, const char *title)
{
    if (conn == NULL || title == NULL)
        return;

    fprintf(stderr, "%s:\nError %u(%s)\n", title, mysql_errno(conn), mysql_error(conn));
}

void ProcessResults(MYSQL *conn, MYSQL_RES *res)
{
    if (conn == NULL || res == NULL)
        return;

    MYSQL_ROW row;

    while ((row = mysql_fetch_row(res)) != NULL)
    {
        for (int i = 0; i < mysql_num_fields(res); i++)
        {
            if (i > 0)
                printf("\t");
            printf("%s", row[i] ? row[i] : "NULL");
        }
        printf(stdout, "\n");
    }

    if (mysql_errno(conn) != 0)
        PrintError(conn, "mysql_fetch_row error");
    else
        printf("Process all results %lu\n", (unsigned long)mysql_num_rows(res));
}

int ProcessResultOne(MYSQL *conn, char *sqlCmd, char *buffer, int colIndex)
{
    LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "ProcessResultOne\n");
    if (conn == NULL || sqlCmd == NULL)
    {
        LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "input param error\n");
        return -1;
    }

    int ret = 0;
    MYSQL_RES *res = NULL;

    do
    {
        // 执行sql语句，执行成功返回0
        if (mysql_query(conn, sqlCmd) != 0)
        {
            LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "mysql_query error\n");
            PrintError(conn, "mysql_query error");
            ret = -1;
            break;
        }

        // 获取结果集
        res = mysql_store_result(conn);
        if (res == NULL)
        {
            LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "mysql_store_result error\n");
            PrintError(conn, "mysql_store_result error");
            ret = -1;
            break;
        }

        //mysql_num_rows接受由mysql_store_result返回的结果结构集，并返回结构集中的行数
        unsigned long line = mysql_num_rows(res);
        if (line == 0)
        {
            LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "mysql_num_rows(res) == 0\n");
            ret = 1;
            break;
        }
        else if(line > 0 && buffer == NULL )
        {
            LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "mysql_num_rows(res) > 0\n");
            ret = 2;
            break;
        }

        MYSQL_ROW row = mysql_fetch_row(res);
        unsigned int numFields = mysql_num_fields(res);
        if(row != NULL)
        {
            if(colIndex < 0 || colIndex >= numFields)
            {
                LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "colIndex error\n");
                ret = -1;
                break;
            }
            strcpy(buffer, row[colIndex]);
        }
    } while (0);

    if(res != NULL)
    {
        mysql_free_result(res);
        res = NULL;
    }
    LOG(MYSQL_LOG_MODULE, MYSQL_LOG_PROC, "buffer: %s\n", buffer);
    return ret;
}